//
//  ViewController.m
//  test
//
//  Created by Ran on 15/10/27.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "ViewController.h"
#import "RanIntroduceViewController.h"

@interface ViewController ()

@property(nonatomic, weak)UIImageView *imgView;
@property(nonatomic, weak)UILabel*timeLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@", NSStringFromCGRect(self.view.bounds));

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 55, 55)];
//    imgView.layer.cornerRadius = 60 / 2.0;
    imgView.clipsToBounds = YES;
    [self.view addSubview:imgView];
    self.imgView = imgView;
    self.imgView.layer.position = CGPointMake(0, 380);
        imgView.image = [UIImage imageNamed:@"icon"];
    self.imgView.layer.backgroundColor = [UIColor blackColor].CGColor;
    self.imgView.layer.opacity = 0.0;
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 30)];
    [self.view addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(showTime)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
}

- (void)showTime
{
    NSDateFormatter *matter = [NSDateFormatter new];
    matter.dateFormat = @"HH:mm:ss";
    self.timeLabel.text = [matter stringFromDate:[NSDate date]];
}
BOOL a;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    [self changePosition];
//    [self changeColor];
//    [self testChange];
//    [self movepath];
//    [self groupA];
    
//    if (a) {
//        [self.imgView.layer removeAllAnimations];
//    }else{
//    [self moveAndRotate];
//    };
//    a = !a;
//    return;
    [self presentViewController:[RanIntroduceViewController new] animated:YES completion:nil];

}

- (void)moveAndRotate
{
    CAKeyframeAnimation *move = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    move.values = @[
                    [NSValue valueWithCGPoint:CGPointMake(150, 100)],
                    [NSValue valueWithCGPoint:CGPointMake(150, 300)],
                    [NSValue valueWithCGPoint:CGPointMake(300, 300)]
                    ];
    move.keyTimes = @[@0, @0.5, @1.0];
    
    
    CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    rotate.byValue = @(M_PI * 3);
    
    CAKeyframeAnimation *opacity = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opacity.values = @[@1.0,@1.0, @10];
    opacity.keyTimes = @[@0, @0.5,@1];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 5;
    group.animations = @[move, rotate, opacity];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    
    [self.imgView.layer addAnimation:group forKey:nil];
}

- (void)groupA
{
    //y轴移动的子动画
    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    move.toValue = @500;
    //旋转的子动画
    CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotate.toValue = @M_PI;
    //动画组
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[move, rotate];
    group.duration = 5;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [self.imgView.layer addAnimation:group forKey:nil];

}

- (void)movepath
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //创建一个路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //画一个半圆
    [path addArcWithCenter:CGPointMake(160, 200) radius:120 startAngle:- M_PI_2 endAngle:M_PI_2 clockwise:YES];
    //画一条直线
    [path addLineToPoint:CGPointMake(160, 500)];
    //路径赋值
    animation.path = path.CGPath;
    animation.duration = 3.0;
    [self.imgView.layer addAnimation:animation forKey:nil];
}

- (void)testChange
{
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
//    ani.toValue = [NSValue valueWithCGRect:CGRectMake(111, 111, 111, 111)];
    ani.toValue = @12;[NSValue valueWithCGPoint:CGPointMake(99, 99)];
    [self.imgView.layer addAnimation:ani forKey:nil];
    return;
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"frame.origin"];
    animation.duration = 5;
    //6个关键帧,起点和终点重合了
    animation.values = @[
                         [NSValue valueWithCGPoint:CGPointMake(0, 118)],
                         [NSValue valueWithCGPoint:CGPointMake(326, 118)],
                         [NSValue valueWithCGPoint:CGPointMake(63, 310)],
                         [NSValue valueWithCGPoint:CGPointMake(164, 0)],
                         [NSValue valueWithCGPoint:CGPointMake(264, 310)],
                         [NSValue valueWithCGPoint:CGPointMake(0, 118)],
                         ];
    //5个帧与帧之间的运动平分了动画执行时间
    animation.keyTimes = @[
                           [NSNumber numberWithFloat:0.0],
                           [NSNumber numberWithFloat:0.2],
                           [NSNumber numberWithFloat:0.4],
                           [NSNumber numberWithFloat:0.6],
                           [NSNumber numberWithFloat:0.8],
                           [NSNumber numberWithFloat:1.0],
                           ];
    //无限循环
    animation.repeatCount = MAXFLOAT;
    [self.imgView.layer addAnimation:animation forKey:nil];
}

- (void)changeMoveMove
{

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 5;
    //6个关键帧,起点和终点重合了
    animation.values = @[
                         [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 118, 0)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(326, 118, 0)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(63, 310, 0)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(164, 0, 0)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(264, 310, 0)],
                         [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 118, 0)],
                         ];
    //5个帧与帧之间的运动平分了动画执行时间
    animation.keyTimes = @[
                           [NSNumber numberWithFloat:0.0],
                           [NSNumber numberWithFloat:0.2],
                           [NSNumber numberWithFloat:0.4],
                           [NSNumber numberWithFloat:0.6],
                           [NSNumber numberWithFloat:0.8],
                           [NSNumber numberWithFloat:1.0],
                           ];
    //无限循环
    animation.repeatCount = MAXFLOAT;
    [self.imgView.layer addAnimation:animation forKey:nil];
}


- (void)changePosition
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    animation.duration = 1;
//    animation.fromValue = @100;
    animation.byValue = @40;
    animation.toValue = @200;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.imgView.layer addAnimation:animation forKey:nil];
}

- (void)changeDegree
{
    //创建一个CABasicAnimation实例,keypath为transform.rotation.z,也就是沿着坐标系中的z轴运动
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //动画执行时间2.0秒
    animation.duration = 2.0;
    //取值类型取决于keypath,这里需要一个角度值,所以是一个NSNumber类型,复制为M_PI即180°
    animation.toValue = @(M_PI);
    //增加两个属性
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    //重复次数
    animation.repeatCount = MAXFLOAT;
    //效果累加
    animation.cumulative = YES;
    //把动画实例加到视图的layer层,动画立刻开始执行
    [self.imgView.layer addAnimation:animation forKey:@"rotate"];
}

- (void)changeColor
{
//    self.view.layer.opacity
//    self.view.layer.backgroundColor
//    self.imgView.layer.backgroundColor = [UIColor blackColor].CGColor;
    
    //添加动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    //单次执行时间
    animation.duration = 2;
    //起始值
    animation.fromValue = (id)[UIColor blackColor].CGColor;
    //终止值
    animation.toValue = (id)[UIColor redColor].CGColor;
    //自动回退
    animation.autoreverses = YES;
    //无限循环
    animation.repeatCount = MAXFLOAT;
    //开始和结束部分变化速度放缓
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.imgView.layer addAnimation:animation forKey:nil];
}


- (IBAction)pause:(id)sender
{
    self.imgView.layer.timeOffset = [self.imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.imgView.layer.speed = 0;
}

- (IBAction)resume:(id)sender
{
    CFTimeInterval pauseStamp = self.imgView.layer.timeOffset;
    self.imgView.layer.speed = 1.0;
    self.imgView.layer.timeOffset = 0.0;
    self.imgView.layer.beginTime = 0;
    self.imgView.layer.beginTime = [self.imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pauseStamp;
}

- (IBAction)removeAnimation:(id)sender
{
    //移除指定动画
    [self.imgView.layer removeAnimationForKey:@"rotate"];
    //移除所有动画
    [self.imgView.layer removeAllAnimations];
    
    CAKeyframeAnimation *ani;
    ani.path;
}

@end
